package telas;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import cliente.Cliente;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class tela_chat extends javax.swing.JFrame {
	private JTextArea chat_textarea;
	public JTextArea getChat_textarea() {
		return chat_textarea;
	}

	public JTextField getChat_textfield() {
		return chat_textfield;
	}

	private JButton enviar_botao;
	private JButton sair_botao;
	private JPanel minha_foto;
	private JLabel nome_amigo_label;
	private JPanel foto_amigo;
	private JTextField chat_textfield;

	public String foto_conversa_amigo;

	/**
	 * Auto-generated main method to display this JFrame
	 */

	Cliente cli;
	//Quem irá receber a mensagem
	String nomeUsuario;

	/*public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				tela_chat inst = new tela_chat();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}*/

	public String getNomeUsuario() {
		return nomeUsuario;
	}

	public tela_chat(String nomeUsuario, Cliente cli, String foto_amigo) {
		super();
		this.nomeUsuario=nomeUsuario;
		this.foto_conversa_amigo=foto_amigo;
		this.cli=cli;	
		initGUI();

		this.cli.setInst(this);
	}

	private void initGUI() {
		try {
			this.setTitle(cli.getUsuario());
			AnchorLayout thisLayout = new AnchorLayout();
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			addWindowListener(new WindowAdapter()  
			{  
				public void windowClosing (WindowEvent e) {  
					//caixa de dialogo retorna um inteiro  
					int resposta = JOptionPane.showConfirmDialog(null,"Deseja finalizar essa operação?","Finalizar",JOptionPane.YES_NO_OPTION);  

					//sim = 0, nao = 1  
					if (resposta == 0) {  

						for (int i = 0; i < cli.telas_bate_papo.size(); i++) {
							if(cli.telas_bate_papo.get(i).getNomeUsuario().equals(nomeUsuario)){
								cli.telas_bate_papo.remove(i);

							}
						}
						//cli.telas_bate_papo.remove(this);
						//Destroi a tela atual
						this.windowClosed(null);
					}  
				}  
			});  
			{
				sair_botao = new JButton();
				getContentPane().add(sair_botao, new AnchorConstraint(943, 159, 987, 52, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				sair_botao.setText("Sair");
				sair_botao.setPreferredSize(new java.awt.Dimension(58, 23));
			}
			{
				minha_foto = new JPanel();
				ImageIcon mfoto = new ImageIcon("foto/"+cli.getFoto()+".png");
				//usuario.setImage(usuario.getImage().getScaledInstance(12, 12, 12));  
				JLabel min_foto = new JLabel(mfoto);
				minha_foto.add(min_foto);
				getContentPane().add(minha_foto, new AnchorConstraint(713, 208, 928, 13, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				minha_foto.setPreferredSize(new java.awt.Dimension(106, 112));
				minha_foto.setBackground(new java.awt.Color(200,200,200));
			}
			{
				nome_amigo_label = new JLabel();
				getContentPane().add(nome_amigo_label, new AnchorConstraint(248, 208, 290, 13, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				nome_amigo_label.setText(nomeUsuario);
				nome_amigo_label.setPreferredSize(new java.awt.Dimension(106, 22));
			}
			{
				foto_amigo = new JPanel();
				ImageIcon amfoto = new ImageIcon("foto/"+foto_conversa_amigo+".png");
				//usuario.setImage(usuario.getImage().getScaledInstance(12, 12, 12));  
				JLabel amin_foto = new JLabel(amfoto);
				foto_amigo.add(amin_foto);
				getContentPane().add(foto_amigo, new AnchorConstraint(14, 208, 236, 13, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				foto_amigo.setPreferredSize(new java.awt.Dimension(106, 116));
				foto_amigo.setBackground(new java.awt.Color(200,200,200));
			}
			{
				enviar_botao = new JButton();
				getContentPane().add(enviar_botao, new AnchorConstraint(943, 988, 987, 828, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				enviar_botao.setText("Enviar");
				enviar_botao.addMouseListener(new MouseAdapter(){  

					public void mouseClicked(MouseEvent e) {
						System.out.println("Nome do usuário a enviar msg:"+nomeUsuario+", msg: "+ chat_textfield.getText());
						cli.instanciaServidor.enviarMensagem(nomeUsuario, cli.getUsuario()+" disse: "+chat_textfield.getText(),cli.getUsuario());
						chat_textarea.append(cli.getUsuario()+" disse: "+chat_textfield.getText()+"\n");
						chat_textfield.setText("");

					}					
				});
				enviar_botao.setPreferredSize(new java.awt.Dimension(87, 23));
			}
			{
				chat_textfield = new JTextField();				
				getContentPane().add(chat_textfield, new AnchorConstraint(943, 817, 987, 219, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				chat_textfield.setPreferredSize(new java.awt.Dimension(325, 23));
				chat_textfield.addKeyListener(new KeyListener() {

					public void keyTyped(KeyEvent e) {

					}

					public void keyReleased(KeyEvent e) {

					}

					public void keyPressed(KeyEvent e) {
						//System.out.println("Pressionado");
						if(e.getKeyCode() == KeyEvent.VK_ENTER){
							System.out.println("Nome do usuário a enviar msg:"+nomeUsuario+", msg: "+ chat_textfield.getText());
							cli.instanciaServidor.enviarMensagem(nomeUsuario, cli.getUsuario()+" disse: "+chat_textfield.getText(),cli.getUsuario());
							chat_textarea.append(cli.getUsuario()+" disse: "+chat_textfield.getText()+"\n");
							chat_textfield.setText("");}						
					}
				});
			}
			{
				chat_textarea = new JTextArea();
				//chat_textarea.setEnabled(false);
				chat_textarea.setEditable(false);
				getContentPane().add(chat_textarea, new AnchorConstraint(14, 988, 930, 219, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				chat_textarea.setPreferredSize(new java.awt.Dimension(418, 478));
				
				
			}
			pack();
			this.setSize(560, 560);
		} catch (Exception e) {
			//add your error handling code here
			e.printStackTrace();
		}
	}

}
